// Flat 5x5 filter, C version

#include <stdio.h>
//#include <OpenGL/gl.h>
#include "readppm.h"

int main( int argc, char** argv) 
{
  unsigned char *image, *out;
  int n, m, i, j, k, l, sumx, sumy, sumz, start, stop;
  
  image = readppm("maskros512.ppm", &n, &m);
  start = GetMicroseconds();

  out = malloc(n*m*3);
  
  // Skip edges (cheat edge tests)
  for(i=2;i<m-2;i++)
    for(j=2;j<n-2;j++)
      {
	// Filter kernel
	sumx=0;sumy=0;sumz=0;
	for(k=-2;k<3;k++)
	  for(l=-2;l<3;l++)
	    {
	      sumx += image[((i+k)*n+(j+l))*3+0];
	      sumy += image[((i+k)*n+(j+l))*3+1];
	      sumz += image[((i+k)*n+(j+l))*3+2];
	    }
	out[(i*n+j)*3+0] = sumx/25;
	out[(i*n+j)*3+1] = sumy/25;
	out[(i*n+j)*3+2] = sumz/25;
      }

  stop = GetMicroseconds();
  printf("time: %0.3f ms\n", ((float)(stop - start))/1000);
  writeppm("out.ppm", n, m, out);
  return 0;
}
